• Check command pod_exec is used to check status of a command run inside Kubernetes pods. Returns OK if exit code is zero, otherwise, returns CRITICAL.
• Spec - pod_exec check command has the following variables:
-> container - Container name in a Kubernetes Pod
-> cmd - Exec command. [Default: ‘/bin/sh’]
-> argv - Exec command arguments. [Format: ‘arg; arg; arg’]
• Execution of this command can result in following states:
-> OK
-> CRITICAL
-> UNKNOWN

# Tutorial
• you need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster.

kubectl create namespace demo

kubectl get namespaces
NAME          STATUS    AGE
default       Active    6h
kube-public   Active    6h
kube-system   Active    6h
demo          Active    4m

# Check status of pods with matching labels
• If a PodAlert will be used check status of pods with matching labels by setting spec.selector field.

cat ./docs/examples/pod-alerts/pod_exec/demo-0.yaml

apiVersion: monitoring.appscode.com/v1alpha1
kind: PodAlert
metadata:
  name: pod-exec-demo-0
  namespace: demo
spec:
  selector:
    matchLabels:
      app: nginx
  check: pod_exec
  vars:
    argv: ls -l /usr
  checkInterval: 30s
  alertInterval: 2m
  notifierSecretName: notifier-config
  receivers:
  - notifier: Mailgun
    state: CRITICAL
    to: ["ops@example.com"]
    
kubectl apply -f ./docs/examples/pod-alerts/pod_exec/demo-0.yaml

kubectl get pods -n demo

kubectl describe podalert -n demo pod-exec-demo-0

# Check status of a specific pod
• If a PodAlert will be used check status of a pod by name by setting spec.podName field.

cat ./docs/examples/pod-alerts/pod_exec/demo-1.yaml

apiVersion: monitoring.appscode.com/v1alpha1
kind: PodAlert
metadata:
  name: pod-exec-demo-1
  namespace: demo
spec:
  podName: busybox
  check: pod_exec
  vars:
    argv: ls -l /usr
  checkInterval: 30s
  alertInterval: 2m
  notifierSecretName: notifier-config
  receivers:
  - notifier: Mailgun
    state: CRITICAL
    to: ["ops@example.com"]
    
kubectl apply -f ./docs/examples/pod-alerts/pod_exec/demo-1.yaml

kubectl get pods -n demo

kubectl describe podalert -n demo pod-exec-demo-1

# Cleaning up
• To cleanup the Kubernetes resources

kubectl delete ns demo

# Link:- https://appscode.com/products/searchlight/5.1.1/guides/pod-alerts/pod_exec/